package com.demo.example.hot100;

import java.util.Scanner;
import java.util.Stack;

public class MergeStackData {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            String[] strings = s.split(" ");
            Stack<Integer> stack = new Stack<>();
            for (int i = 0; i < strings.length; i++) {
                //第i个位置的数
                int tmp = Integer.parseInt(strings[i]);
                stack.push(tmp);
                int first = stack.peek();
                int result = findTheSum(stack, first);
                while (result != -1) {
                    int n = result;
                    while (n != 0) {
                        stack.pop();
                        n--;
                    }
                    stack.push(first * 2);
                    first = stack.peek();
                    result = findTheSum(stack, first);
                }
            }
            while (!stack.isEmpty()) {
                if(stack.size()!=1) {
                    System.out.print(stack.pop() + " ");
                }else{
                    System.out.print(stack.pop());
                }
            }
        }
    }
    //用来判断栈的首位是否会等于下面几项的和
    public static int findTheSum(Stack<Integer> stack,int first){
        Stack<Integer> tmpStack=new Stack<>();
        tmpStack.addAll(stack);
        int[] help=new int[stack.size()];
        int i=0;
        while(!tmpStack.isEmpty()){
            help[i]=tmpStack.pop();
            i++;
        }
        //此时对数组进行遍历
        for(i=1;i<help.length;i++){
            first-=help[i];
            if(first==0){
                return i+1;
            }
        }
        return -1;
    }
}
